/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 30-nov-2010, 15.38.58
 */

package javaapplication1;

/**
 *
 * @author Simone
 *
 *
 *
 */


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.*;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import gov.nasa.jpf.*;


public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea_Results = new javax.swing.JTextArea();
        jButton_TestRapido = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Seleziona la classe per vederne i metodi");

        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setViewportView(jList2);

        

        jButton2.setText("Cerca");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
////////////////////////////
        jButton3.setText("Salva");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            
        });
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });


        
        
        
        
        
        jTextArea_Results.setColumns(20);
        jTextArea_Results.setRows(5);
        jTextArea_Results.setName(""); // NOI18N
        jScrollPane3.setViewportView(jTextArea_Results);
        jTextArea_Results.getAccessibleContext().setAccessibleName("");

        jButton_TestRapido.setText("Test Rapido");
        jButton_TestRapido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_TestRapidoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton_TestRapido)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addGap(18, 18, 18))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton_TestRapido))
                .addGap(4, 4, 4)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        // TODO add your handling code here:
        
      Method[] m = null;
        try {
            Class c = Class.forName("ClassesTest."+jList1.getSelectedValue().toString().replace(".java", ""));
//            Class c = Class.forName("ClassesTest.");
            System.out.println(c.getName());
            
            m = c.getDeclaredMethods();
            
         }
         catch (Throwable e) {
             e.printStackTrace();

         }
         final Method[] m2 = m;
        jList2.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return m2.length; }
            public Object getElementAt(int i) { return m2[i]; }
        });
    }//GEN-LAST:event_jList1ValueChanged

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
           
    	System.out.println(new File(new File("").getAbsolutePath() +File.separator+"src"+ File.separator+"ClassesTest").getAbsolutePath());
       final File[]nomifiles = new File(new File("").getAbsolutePath() +File.separator+"src"+ File.separator+"ClassesTest").listFiles(new ImageFileFilter());
     //  final String []nomiclassi ;
       final String []nomi = new String[nomifiles.length];
       for(int j=0; j<nomifiles.length; j++)
       {
           nomi[j] = nomifiles[j].getName();
       }

       
       
        jList1.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return nomi.length; }
            public Object getElementAt(int i) { return nomi[i]; }
        });
        
        
           //System.out.println(nomefile);


        //System.out.println(System.getProperty("java.class.path"));


    }//GEN-LAST:event_formWindowOpened

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        // TODO add your handling code here:
        


    }//GEN-LAST:event_jButton1ActionPerformed
    
    

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        // TODO add your handling code here:
    	try {
    	      FileOutputStream file = new FileOutputStream(""+ jList1.getSelectedValue()+ "Output.txt");
    	      PrintStream Output = new PrintStream(file);
    	      Output.println("Data creazione Output "+datetimenow("dd/MM/yy hh:mm:ss aaa") +JPFOutput.output);
    	      

    	    } catch (IOException e) {
    	      System.out.println("Errore: " + e);
    	      System.exit(1);
    	    }


    	

    }
    
    

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
//        if(jList2.getSelectedValues())
//        {


       new NewJFrame1().setVisible(true);



    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
          //new FileChooser().setVisible(true);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        new FileChooser().setVisible(true);


    }//GEN-LAST:event_jButton2MouseClicked
    
    private String datetimenow(String dateFormat){
    	Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(cal.getTime());
    }

    private void jButton_TestRapidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_TestRapidoActionPerformed
        // TODO add your handling code here:
//        JOptionPane.showMessageDialog(this, "Fatto!");
//    	Object[] selectedMethods = jList2.getSelectedValues();
    	int[] selectedIndexes = jList2.getSelectedIndices();
    	JPFOutput jpfOutput = new JPFOutput();
    	jpfOutput.reset();
//    	Vector metodiselezionati = new Vector();
//    	for(Object object:selectedMethods){
//    		metodiselezionati.add(object);
//    		jTextArea_Results.append(object.toString()+"\n");
//    	}
    	Method[] metodi = new Method[selectedIndexes.length];
    	for (int i =0; i<selectedIndexes.length; i++){
    		metodi[i]=(Method)jList2.getModel().getElementAt(selectedIndexes[i]);
//    		jTextArea_Results.append(metodi[i].getName());
    	}
    	
    	String classe = new String(jList1.getSelectedValue().toString());
    	classe = classe.substring(0, classe.length()-5);
    	////////////// prova - non definitivo!
    	Class[] classi = new Class[2];
    	try {
        	classi[0] = Class.forName("ClassesTest.NewClass2");
			classi[1] = Class.forName("ClassesTest.NewClass3");
			Method[] NewClass3Methods = classi[1].getDeclaredMethods();
//			metodi = concat(metodi, NewClass3Methods);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("*********************");
		for (Method m: metodi) System.out.println(m.getName()); 		
		System.out.println("*********************");

		///////////////
		MioListener m = new MioListener(metodi);
		String[] args = new String[0];
		Config config = JPF.createConfig(args);
		config.setTarget("ClassesTest."+classe);
		JPF j = new JPF(config);

		j.addListener(m);
		
//		System.out.println(datetimenow("dd/MM/yy hh:mm:ss aaa"));
//		jpfOutput.appendi(datetimenow("dd/MM/yy hh:mm:ss aaa")+"\n");

		System.out.println("#####Inizio Test#####"+ datetimenow("dd/MM/yy hh:mm:ss aaa"));
		jpfOutput.appendi("#####Inizio Test##### "+datetimenow("dd/MM/yy hh:mm:ss aaa")+"\n");
		j.run();
		jpfOutput.appendi("#####Fine Test#####\n");
		System.out.println("#####Fine Test#####");
    	jTextArea_Results.append(jpfOutput.getOutput());
//    	JOptionPane.showMessageDialog(this, metodiselezionati.get(0));
    	
    }//GEN-LAST:event_jButton_TestRapidoActionPerformed

    private Method[] concat(Method[] A, Method[]B){
    	Method[] C = new Method[A.length+B.length];
    	System.arraycopy(A, 0, C, 0, A.length);
    	System.arraycopy(B, 0, C, A.length, B.length);
    	return C;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });

       // jList1.getSelectedIndex(jList2.);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton_TestRapido;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea_Results;
    // End of variables declaration//GEN-END:variables

}
